package com.hydra.auth.repository;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hydra.auth.entity.SysMenu;
import com.hydra.auth.entity.SysOperateLog;
import com.hydra.auth.entity.SysRole;
import com.hydra.auth.mapper.SysOperateLogMapper;
import com.hydra.auth.mapper.SysRoleMapper;
import com.hydra.base.error.BusinessException;
import com.hydra.base.operate.log.annotation.OperateLogInject;
import com.hydra.base.operate.log.entity.BaseOperateLogEntity;
import com.hydra.base.services.BaseService;
import com.hydra.base.utils.StringUtil;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class SysRoleService extends BaseService<SysRole, SysRoleMapper> {

    @Resource
    private SysOperateLogMapper sysOperateLogMapper;

    /**切面进行操作日志记录*/
    @Override
    @OperateLogInject(cate = "SYS_ROLE", codeField="roleCode")
    public void handleOperateLog(SysRole data, SysRole history) {
        super.handleOperateLog(data, history);
    }

    /**保存操作日志*/
    @Override
    public void saveOperateLog(BaseOperateLogEntity operateLogInfo, SysRole updateData, SysRole historyData) {
        SysOperateLog log = operateLogInfo.copy(SysOperateLog.class);
        sysOperateLogMapper.insert(log);
    }

    public List<SysRole> findByRoleCode(String roleCode){
        return this.list(Wrappers.<SysRole>lambdaQuery()
                .eq(SysRole::getRoleCode, roleCode));
    }

    public List<SysRole> findByUserCode(String userCode){
        return this.baseMapper.findByUserCode(userCode);
    }


    /**
     * 保存用户角色信息
     * @param userCode  用户编码
     * @param roleList  角色列表
     */
    public void saveUserRoleInfo(String userCode, List<SysRole> roleList){
        if(StringUtil.isBlank(userCode)){
            throw new BusinessException("用户编码不能为空");
        }
        if(CollectionUtils.isEmpty(roleList)){
            throw new BusinessException("角色编码不能为空");
        }
        this.baseMapper.saveUserRoleInfo(userCode, roleList);
    }

}
